//
//  TaskListViewModel.swift
//  MVVM3
//
//  Created by 李煜 on 2024/7/1.
//

import UIKit

class TaskListViewModel {

    private let taskManager: TaskManager
    var tasks: [GTask] = [] {
        didSet {
            // 当任务列表发生变化时，通知View更新
            delegate?.tasksDidUpdate(tasks)
        }
    }
    weak var delegate: TaskListViewModelDelegate?
    init(taskManager: TaskManager) {
        self.taskManager = taskManager
        self.tasks = taskManager.getTasks()
    }
    func addTask(title: String) {
        let task = GTask(title: title, completed: false)
        taskManager.addTask(task: task)
        tasks.append(task)
    }
    

}
protocol TaskListViewModelDelegate: AnyObject {
    func tasksDidUpdate(_ tasks: [GTask])
}
